/*
 * CVImage.h
 *
 *  Created on: Nov 10, 2008
 *      Author: brain
 */

#ifndef CVIMAGE_H_
#define CVIMAGE_H_

#include "RefCount.h"


class CVImage
{

private:
	template<class integer>
	void pclear(integer value); // used by clear method

public:
	CVImage(IplImage * im);
	CVImage(CVImage & p);
	~CVImage();
	IplImage* operator ->() {return _counter->_data;}
	const IplImage* operator->() const {return _counter->_data;}
	operator IplImage* () { return _counter->_data;}
	operator IplImage* const () const { return _counter->_data;}



	void SaveAs(string filename);//* saves the image by the file name

	int Pixel(uint x,uint y,uchar channel);//* returns the pixel intensity. execute this function only is the depth is IPL_DEPTH_32S
	void *Pix(uint x,uint y,uchar channel);//* returns the intensity of the pixel in the givven channel for every depth (requires casting)
	void *Pix(uint index,uchar channel);//* it works by the pixels's order
	CvScalar getpix(uint x,uint y);//* returns the pixel's intensity, exists for integration with the openCV interface
	void setpix(uint x,uint y,CvScalar s);//* changes the pixel's intensity, exists for integration with the openCV interface
	void ShowImage(string title);//* opens a window that displays the image and sets the window's title
	CVImage FROM_32S_TO_8U();//* converts the image depth. (for display)
	IplImage * ToIplImage();//* returns the pointer of IPL_image for integration with opencv interface
	const IplImage * ToIplImage() const;
	CVImage& operator=(CVImage* cvi);//* assignment operator from pointer.
	CVImage& operator=(CVImage& cvi);//* assignment operator by value reference.
	RefCount *_counter;//* a pointer to the refcounter which contains the data in IPL format

	//* image info
	uint step() const;//* the width step
	uint size() const;//* size of the image in bytes
	uint height() const;//* height of the image
	uint ibits() const;//* the important bits (depth) in the color in uint size: 00000000000000000000000011111111 00000000000000001111111111111111 11111111111111111111111111111111

	uint width() const;//* width of the image
	unsigned char channels() const;//* number of channels in the image
	unsigned int depth() const;//* returns the image's bits depth
	int encdepth() const;//* return the image's depth in the original format
	//* image processing
	void ToGrayscale();//*convets the image to gray
	void clear(int value); //* set all pixels to the given value
	//* graph help
};

#endif /* CVIMAGE_H_ */
